﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Models;
using System.IO;
using System.Collections;
using System.Drawing;

//using DataAccess;
using System.Data;

public partial class client_DataSheet : @base
{
    DataTable components = new DataTable();
    DataTable selectedComponents = new DataTable();
    DataTable data = new DataTable();
    bool change = false;
    DataSheetService ds = new DataSheetService();
    ReportServices rs = new ReportServices();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MachineNameLabel.Text = "Technical Data Sheet for "+ MACHINENAME;
            ComponentDataGridView.Visible = false;
            ComponentDataGridView.Columns.Clear();
        }

    }
    protected void Component_changed(object sender, EventArgs e)
    {

        if (SelectComponent_DropDownList.SelectedValue == "Choose Components")
        {
            if (change == false)
            {
                components = rs.getcomponentsByMachine(MACHINEID);
                change = true;
            }
            foreach (DataRow site in components.Rows)
            {
                String siteName = site["componentName"].ToString();
                string siteID = site["component_id"].ToString();

                System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(siteName, siteID, true);
                ComponentsCheckBoxList.Items.Add(ls);
            }

        }
        else
        {
            ComponentsCheckBoxList.Items.Clear();
        }

    }
    protected DataTable getSelectedComponent()
    {
        DataTable SC = new DataTable();
        SC.Columns.Add("componentName", typeof(string));
        SC.Columns.Add("component_id", typeof(int));
        if (SelectComponent_DropDownList.SelectedValue == "Choose Components")
        {

            foreach (System.Web.UI.WebControls.ListItem lt in ComponentsCheckBoxList.Items)
            {

                if (lt.Selected)
                {
                    string componentName = "";
                    componentName = lt.Text;
                    int componentID = Int32.Parse(lt.Value);
                    SC.Rows.Add(componentName, componentID);
                }
            }
        }
        else
        {
            if (change == false)
            {
                components = rs.getcomponentsByMachine(MACHINEID);
                change = true;
            }
            SC = components;

        }
        return SC;
    }
    protected void bindGridview(DataRow[] selectedComponent)
    {
        #region Code for preparing the DataTable
        DataTable dt = new DataTable();
        DataColumn dcol = new DataColumn("Attribute", typeof(System.String));
        #endregion


        /*
       * #region Code for preparing the DataTable

        //Create an instance of DataTable

        DataTable dt = new DataTable();

        //Create an ID column for adding to the Datatable

        DataColumn dcol = new DataColumn(ID, typeof(System.Int32));

        dcol.AutoIncrement = true;

        dt.Columns.Add(dcol);

        //Create an ID column for adding to the Datatable

     //   dcol = new DataColumn(NAME, typeof(System.String));

        dt.Columns.Add(dcol);

        //Now add data for dynamic columns

        //As the first column is auto-increment we do not have to add any thing.

        //Let's add some data to the second column.

        for (int nIndex = 0; nIndex < 10; nIndex++)

        {

        //Create a new row

        DataRow drow = dt.NewRow();

        //Initialize the row data.

        //drow[NAME] = Row + Convert.ToString((nIndex + 1));

        //Add the row to the datatable.

        dt.Rows.Add(drow);

        }

        #endregion*
       * 
       */
       // ComponentDataGridView.Columns.Add(
    }
    protected void initTable()
    {
        DataTable dt = new DataTable();

        //Create an ID column for adding to the Datatable

        
        //Create an ID column for adding to the Datatable

        //   dcol = new DataColumn(NAME, typeof(System.String));

       
    }
   

    protected void GetDataSheet(object sender, EventArgs e)
    {
        TextBox3.Text = "hichic";
        selectedComponents = getSelectedComponent();
        ComponentDataGridView.Dispose();
        ComponentDataGridView.Columns.Clear();
        ComponentDataGridView.DataSource = null;
        ComponentDataGridView.DataBind();
        #region Code for preparing the DataTable
        DataTable dt = new DataTable();
        DataColumn dcol = new DataColumn("Attribute", typeof(System.String));
        dt.Columns.Add(dcol);
        dcol = new DataColumn("Value", typeof(System.String));
        dt.Columns.Add(dcol);        

        data = ds.getDataByComponentID(selectedComponents);
        //initTable();
        List<int> indexes = new List<int>();
        int i=-1;
        foreach (DataRow r in selectedComponents.Rows)
        {
            int componentId = ToInt(r["component_id"].ToString());            
            DataRow[] componentData = data.Select("component_id = " + componentId);
           // bindGridview(selectedComponent);
            DataRow name = dt.NewRow();
            name["Attribute"] = r["componentName"];
            name["Value"] = "";                
            dt.Rows.Add (name);
            i++;
            indexes.Add(i);
            foreach(DataRow row in componentData)
            {
                i++;
                DataRow drow = dt.NewRow();
                drow["Attribute"] = row["Attribute"]+":";
                drow["Value"] = row["Value"];
                dt.Rows.Add(drow);

            }
        }
        ComponentDataGridView.Columns.Clear();
        

 /*       foreach (DataColumn col in dt.Columns)

        {

            //Declare the bound field and allocate memory for the bound field.

            BoundField bfield = new BoundField();

            //Initalize the DataField value.

            bfield.DataField = col.ColumnName;

            
            //Add the newly created bound field to the GridView.

            ComponentDataGridView.Columns.Add(bfield);

         }
        */
        //Initialize the DataSource
      //  ViewState["CurrentTable"] = dt;
        ComponentDataGridView.AutoGenerateColumns = true;
        ComponentDataGridView.DataSource = dt;

        //Bind the datatable with the GridView.

        ComponentDataGridView.DataBind();
        ComponentDataGridView.Width = 500;
        ComponentDataGridView.BorderColor = Color.White;
        //ComponentDataGridView.Font.
        
        ComponentDataGridView.HeaderRow.Visible = false;
        int ind = 0;
        foreach (GridViewRow gridrow in ComponentDataGridView.Rows)
        {
            ComponentDataGridView.Rows[ind].HorizontalAlign = HorizontalAlign.Left;
            ComponentDataGridView.Rows[ind].Cells[0].ForeColor = Color.SeaGreen;
            ind++; 
        }
        foreach (int inn in indexes)
        {
            ComponentDataGridView.Rows[inn].Cells[0].ForeColor = Color.Red;
            ComponentDataGridView.Rows[inn].Font.Bold = true;
            ComponentDataGridView.Rows[inn].Font.Size = 13;
             ComponentDataGridView.Rows[inn].HorizontalAlign = HorizontalAlign.Center;
        }
        ComponentDataGridView.HorizontalAlign = HorizontalAlign.Center;
        ComponentDataGridView.Visible = true;
     //   ComponentDataGridView.EnableViewState = true;
       
        #endregion

        if (UserDetails.Role == "Admin")
        {
            Edit.Visible = true;
            
            
        }
    }
    
    protected void Edit_Click(object sender, EventArgs e)
    {

        TextBox3.Enabled = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ComponentDataGridView.EnableViewState = true;
        TextBox3.Enabled = true;
    }
}

